package org.jsbd.boss.task;

import org.apache.commons.lang.time.DateFormatUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IDmChannelRetentDAO;
import org.jsbd.boss.domian.channel.RetentTaskVo;
import org.jsbd.boss.mq.LogProcessor;
import org.jsbd.boss.service.LogManager;
import org.jsbd.boss.service.impl.CacheParamManager;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Date;

/**
 * @desc 更新拉活记录
 * @Date 2018/10/26 14:54
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
public class RetentBrushUpdateTask implements Runnable, Serializable {

    public static final Logger logger = LoggerFactory.getLogger(RetentBrushUpdateTask.class);

    private static final long serialVersionUID = 1L;

    private RetentTaskVo taskVo;

    public RetentBrushUpdateTask(RetentTaskVo taskVo) {
        this.taskVo = taskVo;
    }


    @Override
    public void run() {
        String imsi = taskVo.getImsi();
        LogManager logManager = SpringContextUtils.getBean(LogManager.class);
        RedisClient redisClient = SpringContextUtils.getBean("redisClient");
        Integer apkId = taskVo.getApkId();
        Integer brushCode = taskVo.getBrushCode();
        Integer brushId = taskVo.getBrushId();
        String brushName = taskVo.getBrushName();
        String brushUrl = taskVo.getBrushUrl();
        Integer brushDay = taskVo.getBrushDay();
        String mark = taskVo.getMark();
        String lockKey = CacheKey.RETENT_LOCK + ":" + brushId + ":" + apkId + ":" + brushDay + ":" + imsi;
        boolean lock = redisClient.evalSetNx(lockKey, 180);
        logManager.printLog(logger, "拉活记录 brushId {} brushCode {} imsi {} brushName {}  brushUrl {} brushDay {} apkId {} lock {}", new Object[]{brushId, brushCode, imsi, brushName, brushUrl, brushDay, apkId, lock});
        if (!lock) {
            return;
        }

        if (!(apkId == null || brushCode == null || brushId == null || brushDay == null || imsi == null || mark == null)) {
            String expireTime = (String) SpringContextUtils.getBean(CacheParamManager.class).getParamValue("BRUSH_IMSI_RECORD_TIME");
            if (expireTime == null) {
                expireTime = "86400";
            }
            if (!redisClient.evalSetNx(CacheKey.RETENT_BRUSH_LOCK + brushDay.toString() + taskVo.getImsi() + taskVo.getApkId(), Long.parseLong(expireTime))) {
                return;
            }

            taskVo.setBrushTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            SpringContextUtils.getBean(IDmChannelRetentDAO.class).updateRetentTimes(taskVo);
            redisClient.evalHincrby(CacheKey.BRUSH_RECORD, brushDay.toString() + brushId + "realExecTotal", 1);
            redisClient.evalHincrby(CacheKey.BRUSH_RECORD + brushDay + brushId, brushCode + "-" + apkId.toString() + "realExecTotal", 1);
            LogProcessor.getTM().addReturnLinkLog(brushDay.toString(), brushId.toString());
        }
    }

}
